package com.walter.security;

import com.walter.entity.User;
import com.walter.exception.LogicException;
import com.walter.mapper.UserMapper;
import com.walter.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * exam Created by ch on 2019-07-03.
 * FOR:
 */
@Component
@Slf4j
public class Jwt implements Serializable {
    public static  Object currentId(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        }
        Object userName  = authentication.getPrincipal();
        UserMapper userMapper = SpringUtil.getBean(UserMapper.class);
        User user = userMapper.selectOne(new User().setUserName((String) userName));
        if (null == user){
            log.info("user is null");
            throw new LogicException("当前用户为空");
        }
        return user.getId();
    }
}
